package com.campus.util;

import cn.hutool.core.util.BooleanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Objects;
import java.util.concurrent.TimeUnit;


@Component
public class RedisMutex {

    @Autowired
    private StringRedisTemplate redisTemplate;


    public boolean setMutex(int floor,int rows,int column,String accout){
        String key = "mutex:"+floor+":"+rows+":"+column;
        Boolean success = redisTemplate.opsForValue().setIfAbsent(key, accout,30L, TimeUnit.MINUTES);
        return BooleanUtil.isTrue(success);
    }
    public boolean delMutex(int floor,int rows,int column){
        String key = "mutex:"+floor+":"+rows+":"+column;
        Boolean delete = redisTemplate.delete(key);
        return Boolean.TRUE.equals(delete);
    }
}
